﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.Customization;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Columns;
using DTO;
using BUS;

namespace GUI
{
    public partial class LapBaoCaoTháng_GUI : DevExpress.XtraEditors.XtraUserControl
    {
        public LapBaoCaoTháng_GUI()
        {
            InitializeComponent();
        }

        List<DoanhThuThang_DTO> listThang = DoanhThuThang_BUS.GetDoanhThuThangList();
        List<DoanhThuNam_DTO> listNam = DoanhThuNam_BUS.GetDoanhThuNamList();
        BindingList<ChiTietDoanhThuThang_DTO> listChiTiet = new BindingList<ChiTietDoanhThuThang_DTO>();

        string CreateMaDoanhThuThang()
        {
            string Kq = "DTT0";
            char[] c = "DTT0".ToCharArray();
            if (listThang.Count == 0)
            {
                Kq = "DTT01";
            }
            else
            {
                string Ma = DoanhThuThang_BUS.MaDoanhThuThangMax();
                Ma = Ma.TrimStart(c);
                Kq += Convert.ToString(Convert.ToInt32(Ma) + 1);
            }
            return Kq;
        }
        string CreateMaDoanhThuNam()
        {
            string Kq = "DTN0";
            char[] c = "DTN0".ToCharArray();
            if (listNam.Count == 0)
            {
                Kq = "DTN01";
            }
            else
            {
                string Ma = DoanhThuNam_BUS.MaxMaDoanhThuMan();
                Ma = Ma.TrimStart(c);
                Kq += Convert.ToString(Convert.ToInt32(Ma) + 1);
            }
            return Kq;
        }
        string CreateMaChiTietDoanhThu()
        {
            string Kq = "CTDT0";
            char[] c = "CTDT0".ToCharArray();
            BindingList<ChiTietDoanhThuThang_DTO> list = ChiTietDoanhThuThang_BUS.GetChiTietList();
            if (list.Count == 0)
            {
                Kq = "CTDT01";
            }
            else
            {
                string Ma = ChiTietDoanhThuThang_BUS.MaChiTietMax();
                Ma = Ma.TrimStart(c);
                Kq += Convert.ToString(Convert.ToInt32(Ma) + 1);
            }
            return Kq;
        }

        private void btnBaoCao_Click(object sender, EventArgs e)
        {
            DoanhThuNam_DTO mNam = new DoanhThuNam_DTO();
            DoanhThuThang_DTO mThang = new DoanhThuThang_DTO();
            if (DoanhThuNam_BUS.KiemTra(int.Parse(txtNam1.Text)) == false)
            {
                mNam.MaDoanhNam = CreateMaDoanhThuNam();
                mNam.Nam = int.Parse(txtNam1.Text);
                mNam.TongDoanhThuNam = 0;
                DoanhThuNam_BUS.InsertDoanhThuNam(mNam);

                listChiTiet = ChiTietDoanhThuThang_BUS.GetBaoCaoThangList(int.Parse(cmbThang.Text), int.Parse(txtNam1.Text));
                gridC_BaoCaoDoanhThu.DataSource = listChiTiet;
                int mTongVeThang = 0;
                int mTongVeDatThang = 0;
                float mTongDoanhThu = 0;
                for (int i = 0; i < listChiTiet.Count; i++)
                {
                    mTongVeThang += listChiTiet[i].TongSoVe;
                    mTongVeDatThang += listChiTiet[i].SoVe;
                    mTongDoanhThu += listChiTiet[i].DoanhThu;
                }

                mThang.MaDoanhThuThang = CreateMaDoanhThuThang();
                mThang.Thang = int.Parse(cmbThang.Text);
                mThang.MaDoanhThuNam = mNam.MaDoanhNam;
                mThang.SoChuyenBay = listChiTiet.Count;
                mThang.TyLe = (mTongVeDatThang * 100) / mTongVeThang;
                mThang.TongDoanhThuThang = mTongDoanhThu;
                DoanhThuThang_BUS.InsertDoanhThuThang(mThang);
                mNam.TongDoanhThuNam = mNam.TongDoanhThuNam + mThang.TongDoanhThuThang;
                DoanhThuNam_BUS.UpdateDoanhThuNam(mNam);

                for (int i = 0; i < listChiTiet.Count; i++)
                {
                    ChiTietDoanhThuThang_DTO chitiet = new ChiTietDoanhThuThang_DTO();
                    chitiet.MaChiTietDoanhThu = CreateMaChiTietDoanhThu();
                    chitiet.MaDoanhThuThang = mThang.MaDoanhThuThang;
                    chitiet.MaChuyenBay = listChiTiet[i].MaChuyenBay;
                    chitiet.SoVe = listChiTiet[i].SoVe;
                    chitiet.TyLe = listChiTiet[i].TyLe;
                    chitiet.DoanhThu = listChiTiet[i].DoanhThu;
                    ChiTietDoanhThuThang_BUS.InsertChiTiet(chitiet);
                }
            }
            else
            {
                mNam = DoanhThuNam_BUS.GetDoanhThuNamDetails(int.Parse(txtNam1.Text));
                if (DoanhThuThang_BUS.KiemTra(int.Parse(cmbThang.Text), mNam.MaDoanhNam) == false)
                {
                    listChiTiet = ChiTietDoanhThuThang_BUS.GetBaoCaoThangList(int.Parse(cmbThang.Text), int.Parse(txtNam1.Text));
                    gridC_BaoCaoDoanhThu.DataSource = listChiTiet;
                    int mTongVeThang = 0;
                    int mTongVeDatThang = 0;
                    float mTongDoanhThu = 0;
                    for (int i = 0; i < listChiTiet.Count; i++)
                    {
                        mTongVeThang += listChiTiet[i].TongSoVe;
                        mTongVeDatThang += listChiTiet[i].SoVe;
                        mTongDoanhThu += listChiTiet[i].DoanhThu;
                    }

                    mThang.MaDoanhThuThang = CreateMaDoanhThuThang();
                    mThang.Thang = int.Parse(cmbThang.Text);
                    mThang.MaDoanhThuNam = mNam.MaDoanhNam;
                    mThang.SoChuyenBay = listChiTiet.Count;
                    mThang.TyLe = (mTongVeDatThang * 100) / mTongVeThang;
                    mThang.TongDoanhThuThang = mTongDoanhThu;
                    DoanhThuThang_BUS.InsertDoanhThuThang(mThang);
                    mNam.TongDoanhThuNam = mNam.TongDoanhThuNam + mThang.TongDoanhThuThang;
                    DoanhThuNam_BUS.UpdateDoanhThuNam(mNam);

                    for (int i = 0; i < listChiTiet.Count; i++)
                    {
                        ChiTietDoanhThuThang_DTO chitiet = new ChiTietDoanhThuThang_DTO();
                        chitiet.MaChiTietDoanhThu = CreateMaChiTietDoanhThu();
                        chitiet.MaDoanhThuThang = mThang.MaDoanhThuThang;
                        chitiet.MaChuyenBay = listChiTiet[i].MaChuyenBay;
                        chitiet.SoVe = listChiTiet[i].SoVe;
                        chitiet.TyLe = listChiTiet[i].TyLe;
                        chitiet.DoanhThu = listChiTiet[i].DoanhThu;
                        ChiTietDoanhThuThang_BUS.InsertChiTiet(chitiet);
                    }
                }
                else
                {
                    mThang = DoanhThuThang_BUS.GetDoanhThuThangDetails(int.Parse(cmbThang.Text), mNam.MaDoanhNam);
                    listChiTiet = ChiTietDoanhThuThang_BUS.GetChiTieTheoThangtList(mThang.MaDoanhThuThang);
                    gridC_BaoCaoDoanhThu.DataSource = listChiTiet;
                }
            }
        }

        private void btnXuatEx_Click(object sender, EventArgs e)
        {
            GridView view = gridC_BaoCaoDoanhThu.MainView as GridView;
            if (view != null)
            {
                view.ExportToPdf("Báo Cáo Doanh Thu Tháng.pdf");
                Process pdfExport = new Process();
                pdfExport.StartInfo.FileName = "AcroRd32.exe";
                pdfExport.StartInfo.Arguments = "Báo Cáo Doanh Thu Tháng.pdf";
                pdfExport.Start();
            }
        }

    }
}
